package timesheet.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import timesheet.model.Event;
import timesheet.model.EventTime;
import timesheet.model.Project;
import timesheet.repository.EventRepository;
import timesheet.repository.EventTimeRepository;
import timesheet.repository.ProjectRepository;
import timesheet.web.ContextUtil;

@Controller
@Transactional
public class ProjectController {
	
	@Autowired ProjectRepository projectRepository;
	@Autowired EventRepository eventRepository;
	@Autowired EventTimeRepository eventTimeRepository;
	
	@RequestMapping("/projectlist")
	public ModelAndView project(){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			ModelAndView mv = new ModelAndView("projectlist");
			List<Project> projectList = projectRepository.getAllProject();
			mv.addObject("projectlist", projectList);
			return mv;
		}
		else{return new ModelAndView("page-not-found");}
	}
	
	@RequestMapping("/projectdetail")
	public ModelAndView projectDetail(@RequestParam("id") Long id){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			ModelAndView mv = new ModelAndView("projectdetail");
			Project project = projectRepository.find(id);
			mv.addObject("project", project);
			return mv;
		}
		else{return new ModelAndView("page-not-found");}
	}
	
	@RequestMapping("projectcreate")
	public String projectCreate(){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){	
			return "projectcreate";
		}
		else{return "page-not-found";}
	}
	
	@RequestMapping("/projectcreatesubmit")
	public ModelAndView projectcreatesubmit(@RequestParam("project") String desc){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
		if(projectRepository.findByName(desc)==null){
				Project project = new Project(desc);
				projectRepository.createProject(project);
				return project();
			}
			else{
				ModelAndView mv = new ModelAndView("projectcreate");
				mv.addObject("error","this project has already been created");
				mv.addObject("projectname", desc);
				return mv;
			}
		}
		else{return new ModelAndView("page-not-found");}
	}
	
	@RequestMapping("/projectupdate")
	public ModelAndView projectUpdate(@RequestParam("id") Long id, @RequestParam("project") String desc){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			Project project = projectRepository.find(id);
			project.setProjectName(desc);
			projectRepository.merge(project);
			return project();
		}
		else{return new ModelAndView("page-not-found");}
	}
	@RequestMapping("/projectdelete")
	public ModelAndView projectdelete(@RequestParam("id") Long id){
		if("admin".equals(ContextUtil.getHttpSession().getAttribute("ROLE"))){
			String projectid = String.valueOf(id);
			Project project = projectRepository.find(id);
			List<Event> eventlist = eventRepository.getAllEventByproject(project);
			List<EventTime> eventTimeList = eventTimeRepository.getUserByProject(project);
			for(Event e:eventlist){
				eventRepository.remove(e);
			}
			for(EventTime et:eventTimeList){
				eventTimeRepository.remove(et);
			}
			projectRepository.remove(project);
			return project();
		}
		else{return new ModelAndView("page-not-found");}
	}
}
